package cn.edu.ujn.lab3.c;

import java.util.List;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.ujn.lab3.dao.BaseDict;
import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.service.IBaseDictService;
import cn.edu.ujn.lab3.service.IUserService;

@Controller
public class UserController {

	@Autowired
	private IUserService userService;
	@Autowired
	private IBaseDictService baseDictService;
	

	@RequestMapping("/login.action")
	public String login(String usercode,String password,Model model,HttpSession session) {
		User findUser = this.userService.findUser(usercode, password);
		if (findUser != null) {
			// 客户来源
			List<BaseDict> fromType = baseDictService
					.findBaseDictByTypeCode("002");
			// 客户所属行业
			List<BaseDict> industryType = baseDictService
					.findBaseDictByTypeCode("001");
			// 客户级别
			List<BaseDict> levelType = baseDictService
					.findBaseDictByTypeCode("006");
		
			session.setAttribute("USER_SESSION", findUser);
			return "customer";
		}
		model.addAttribute("msg", "用户名或密码错误，请重新登陆！");
		return "login";
	} 
	
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "login";
	}
	
	@RequestMapping("/logout.action")
	public String logout(HttpSession session) {
		//清楚session
		session.invalidate();
		//重定向到登陆页面
		return "redirect:toLogin";
	}
	
	
}
